from sqlalchemy.orm import Session

from crud import PermissionAdminCrud
from schema.ParamSchema import PortMenusInfo
from util.ErrorCode import ErrorCode


class PermissionAdminController:
    @staticmethod
    def get_menus(port_id: int, db: Session):
        menus_list = []
        add_last_level1 = True
        response = PermissionAdminCrud.get_menus(port_id, db)
        if response.get('code') == 0:
            menus = response.get('data')
            if menus is None:
                return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'data': []}
            else:
                menu_dict = {'function_path_list': [], 'submenus': []}
                sub_menu_dict = {'function_path_list': []}
                menus_len = len(menus)
                for menu in menus:
                    if menu.level == 1:
                        if 'menu_id' in menu_dict:
                            if menu_dict.get('menu_id') != menu.menu_id:
                                menus_list.append(menu_dict)

                                menu_dict = {'function_path_list': [], 'submenus': [], 'menu_id': menu.menu_id,
                                             'parent_id': menu.parent_id, 'menu_name': menu.menu_name}

                        else:
                            menu_dict = {'function_path_list': [], 'submenus': [], 'menu_id': menu.menu_id,
                                         'parent_id': menu.parent_id, 'menu_name': menu.menu_name}

                        if menu.port_menu is None:
                            menu_dict['function_path_list'].append(
                                {'function_path': menu.function_path, 'flag': 0})
                        else:
                            menu_dict['function_path_list'].append(
                                {'function_path': menu.function_path, 'flag': menu.port_menu.flag})

                    elif menu.level == 2:
                        if add_last_level1:
                            menus_list.append(menu_dict)
                            add_last_level1 = False

                        for m in menus_list:
                            if menu.parent_id == m['menu_id']:
                                if 'menu_id' in sub_menu_dict:
                                    if sub_menu_dict.get('menu_id') != menu.menu_id:
                                        m['submenus'].append(sub_menu_dict)

                                        sub_menu_dict = {'function_path_list': [], 'menu_id': menu.menu_id,
                                                         'parent_id': menu.parent_id, 'menu_name': menu.menu_name}
                                else:
                                    sub_menu_dict = {'function_path_list': [], 'menu_id': menu.menu_id,
                                                     'parent_id': menu.parent_id, 'menu_name': menu.menu_name}
                                if menu.port_menu is None:
                                    sub_menu_dict['function_path_list'].append(
                                        {'function_path': menu.function_path, 'flag': 0})
                                else:
                                    sub_menu_dict['function_path_list'].append(
                                        {'function_path': menu.function_path, 'flag': menu.port_menu.flag})

                                if menus_len == 0:
                                    m['submenus'].append(sub_menu_dict)
                                break
                    menus_len -= 1

            return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'data': menus_list}
        else:
            return response

    @staticmethod
    def set_menus(portMenusInfo: PortMenusInfo, db: Session):
        return PermissionAdminCrud.batch_port_menu(portMenusInfo.port_id, portMenusInfo.menus, db)
